package com.anpy.security.controller;

import com.anpy.security.dto.TRoleDTO;
import com.anpy.security.entity.TRole;
import com.anpy.security.service.TRoleService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/api/roles")
public class TRoleController {

    private final TRoleService roleService;

    @Autowired
    public TRoleController(TRoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 创建角色
     *
     * @param roleDTO 角色数据传输对象
     * @return 创建后的角色对象
     */
    @PostMapping
    public ResponseEntity<TRoleDTO> createRole(@Valid @RequestBody TRoleDTO roleDTO) {
        TRole role = roleDTO.toEntity();
        TRole createdRole = roleService.createRole(role);
        return new ResponseEntity<>(TRoleDTO.fromEntity(createdRole), HttpStatus.CREATED);
    }

    /**
     * 更新角色
     *
     * @param id      角色ID
     * @param roleDTO 角色数据传输对象
     * @return 更新后的角色对象
     */
    @PutMapping("/{id}")
    public ResponseEntity<TRoleDTO> updateRole(@PathVariable Integer id, @Valid @RequestBody TRoleDTO roleDTO) {
        TRole role = roleDTO.toEntity();
        TRole updatedRole = roleService.updateRole(id, role);
        return ResponseEntity.ok(TRoleDTO.fromEntity(updatedRole));
    }

    /**
     * 获取角色详情
     *
     * @param id 角色ID
     * @return 角色对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<TRoleDTO> getRoleById(@PathVariable Integer id) {
        TRole role = roleService.getRoleById(id);
        return ResponseEntity.ok(TRoleDTO.fromEntity(role));
    }

    /**
     * 获取所有角色
     *
     * @return 角色列表
     */
    @GetMapping
    public ResponseEntity<List<TRoleDTO>> getAllRoles() {
        List<TRole> roles = roleService.getAllRoles();
        List<TRoleDTO> roleDTOs = roles.stream()
                .map(TRoleDTO::fromEntity)
                .collect(Collectors.toList());
        return ResponseEntity.ok(roleDTOs);
    }

    /**
     * 获取所有启用的角色
     *
     * @return 启用的角色列表
     */
    @GetMapping("/active")
    public ResponseEntity<List<TRoleDTO>> getActiveRoles() {
        List<TRole> roles = roleService.getActiveRoles();
        List<TRoleDTO> roleDTOs = roles.stream()
                .map(TRoleDTO::fromEntity)
                .collect(Collectors.toList());
        return ResponseEntity.ok(roleDTOs);
    }

    /**
     * 更新角色状态
     *
     * @param id     角色ID
     * @param status 状态
     * @return 更新后的角色对象
     */
    @PatchMapping("/{id}/status")
    public ResponseEntity<TRoleDTO> updateRoleStatus(@PathVariable Integer id, @RequestParam Boolean status) {
        TRole role = roleService.updateRoleStatus(id, status);
        return ResponseEntity.ok(TRoleDTO.fromEntity(role));
    }

    /**
     * 删除角色
     *
     * @param id 角色ID
     * @return 无内容响应
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRole(@PathVariable Integer id) {
        roleService.deleteRole(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 处理角色相关异常
     *
     * @param ex 异常
     * @return 错误信息
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) {
        return ResponseEntity.badRequest().body(ex.getMessage());
    }
}
